package MyFitnessPlate.TestApplication;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import MyFitnessPlate.DataSet.ParsedAccountDataSet;
import MyFitnessPlate.Handler.AccountHandler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class CreateAccountActivity extends Activity {
	String userName, passWord, firstName, lastName, email, age, gender, height, weight= null;
	Integer feetAdd, inchesAdd=null;
	ParsedAccountDataSet account = new ParsedAccountDataSet();
	 
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_account_screen);
        final Button button = (Button) findViewById(R.id.create_account_button);
        final EditText userEdit = (EditText)findViewById(R.id.userNameEdit);
		final EditText passEdit = (EditText)findViewById(R.id.passwordEdit);
		final EditText firstNameEdit = (EditText)findViewById(R.id.firstNameEdit);
		final EditText lastNameEdit = (EditText)findViewById(R.id.lastNameEdit);
		final EditText emailAddressEdit = (EditText)findViewById(R.id.emailAddressEdit);
		final EditText ageEdit = (EditText)findViewById(R.id.ageEdit);
		final Spinner genderSpinner = (Spinner) findViewById(R.id.genderSpinner);
		final Spinner feetSpinner = (Spinner) findViewById(R.id.feetSpinner);
		final Spinner inchesSpinner = (Spinner) findViewById(R.id.inchesSpinner);
		final EditText weightEdit = (EditText)findViewById(R.id.weightEdit);
        //populate spinners. gender
        ArrayAdapter<CharSequence> genderAdapter = ArrayAdapter.createFromResource( this, R.array.gender, android.R.layout.simple_spinner_item); genderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		genderSpinner.setAdapter(genderAdapter);
		//feet
		ArrayAdapter<CharSequence> feetAdapter = ArrayAdapter.createFromResource( this, R.array.feet_array, android.R.layout.simple_spinner_item); feetAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		feetSpinner.setAdapter(feetAdapter);
		//inches
		ArrayAdapter<CharSequence> inchesAdapter = ArrayAdapter.createFromResource( this, R.array.inches_array, android.R.layout.simple_spinner_item); inchesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		inchesSpinner.setAdapter(inchesAdapter);
		
        
        button.setOnClickListener(new View.OnClickListener(){
        	public void onClick(View v){
        		userName=userEdit.getText().toString().trim();
        		passWord=passEdit.getText().toString().trim();
        		firstName=firstNameEdit.getText().toString().trim();
        		lastName=lastNameEdit.getText().toString().trim();
        		email=emailAddressEdit.getText().toString().trim();
        		age=ageEdit.getText().toString().trim();
        		gender=genderSpinner.getSelectedItem().toString().trim();
        		weight=weightEdit.getText().toString().trim();
        		
        		feetAdd=Integer.parseInt(feetSpinner.getSelectedItem().toString().trim());
        		inchesAdd=Integer.parseInt(inchesSpinner.getSelectedItem().toString().trim());
        		
        		height=String.valueOf((feetAdd*12) + inchesAdd);
        		
        		URL url;
				try {
					url = new URL("http://myfitnessplate.com/api/create-account/username/" +
							 userName + "/password/"+ passWord+ "/email/"+email+ "/first/" + firstName + "/last/" + lastName + 
							 "/age/"+age+"/gender/"+gender+"/weight/"+weight+"/height/"+height+ "/key/HWGCF-YASLT-HOSEC-OZWJH-HETKJ");
				
					/* Get a SAXParser from the SAXPArserFactory. */
					SAXParserFactory spf = SAXParserFactory.newInstance();
					SAXParser sp = spf.newSAXParser();
					/* Get the XMLReader of the SAXParser we created. */
					XMLReader xr = sp.getXMLReader();
					/* Create a new ContentHandler and apply it to the XML-Reader */
					AccountHandler myAccountHandler = new AccountHandler();
					xr.setContentHandler(myAccountHandler);
					/* Parse the xml-data from our URL. */
					xr.parse(new InputSource(url.openStream()));
					/* Parsing has finished. */
					/* Our ExampleHandler now provides the parsed data to us. */
					account = myAccountHandler.getParsedData();
				
					if(account.getError().equalsIgnoreCase("none")){
						final AlertDialog createdDialog = new AlertDialog.Builder(CreateAccountActivity.this).create();
						createdDialog.setTitle("Success");
						createdDialog.setMessage("Account created!.");
						createdDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    		public void onClick(DialogInterface dialog, int which) {
                    			createdDialog.dismiss();
                    			CreateAccountActivity.this.finish(); }
                    	});
						createdDialog.show();
					}
					if(account.getError().equalsIgnoreCase("username")){
						final AlertDialog errorDialog = new AlertDialog.Builder(CreateAccountActivity.this).create();
						errorDialog.setTitle("Error");
						errorDialog.setMessage("Account already exists, try again please.");
						errorDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    		public void onClick(DialogInterface dialog, int which) {
                    			errorDialog.dismiss();
                    			userEdit.setText("");
                    			passEdit.setText(""); }
                    	});
						errorDialog.show();
					}
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					e.printStackTrace();
				} catch (SAXException e) {
					e.printStackTrace();
				}	
        	}
        });
	}
}